//
//  HomeEditViewController.swift
//  fruit-helper-swift
//
//  Created by 赵隆杰 on 2022/6/13.
//

import UIKit
import Toast

class HomeEditViewController: BaseViewController, UITextViewDelegate {
    
    private var bgView: UIView!
    private var imageView: UIImageView!
    private var book_titleLabel : UILabel!
    private var book_authorLabel : UILabel!
    private var contentTextView: UITextView!
    
    private var book_shiyan: NSString = ""
    
    var model: BookModel?

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .clear
        
        setupUI()
        setupData()
        
    }
    
    
    
    
    
}

//设置UI
extension HomeEditViewController {
    
    private func setupUI() {
        bgView = UIView()
        bgView.backgroundColor = .white
        view.addSubview(bgView)
        bgView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalToSuperview().offset(200)
        }
        
        let closeBtn = UIButton()
        closeBtn.setImage(UIImage(named: "close"), for: .normal)
        closeBtn.addTarget(self, action: #selector(dismissBtnClick), for: .touchUpInside)
        bgView.addSubview(closeBtn)
        closeBtn.snp.makeConstraints { make in
            make.width.height.equalTo(50)
            make.right.equalToSuperview().offset(-10)
            make.top.equalToSuperview().offset(10)
        }
        
        let centerTitleLabel = UILabel()
        centerTitleLabel.text = "开始阅读"
        centerTitleLabel.font = UIFont.boldSystemFont(ofSize: 20)
        bgView.addSubview(centerTitleLabel)
        centerTitleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalTo(closeBtn)
        }
        
        imageView = UIImageView()
        imageView.layer.cornerRadius = 5
        imageView.layer.masksToBounds = true
        imageView.contentMode = .scaleAspectFit
        bgView.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.top.equalTo(closeBtn.snp.bottom).offset(10)
            make.height.equalTo(150)
            make.width.equalTo(imageView.snp.height).multipliedBy(640.0/749.0)
            make.centerX.equalToSuperview()
        }
        
        book_titleLabel = UILabel()
        book_titleLabel.font = UIFont.boldSystemFont(ofSize: 18)
        book_titleLabel.textColor = kThemeColor
        bgView.addSubview(book_titleLabel)
        book_titleLabel.snp.makeConstraints { make in
            make.top.equalTo(imageView.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
        }
        book_authorLabel = UILabel()
        book_authorLabel.font = UIFont.boldSystemFont(ofSize: 13)
        book_authorLabel.textColor = kColor333333
        bgView.addSubview(book_authorLabel)
        book_authorLabel.snp.makeConstraints { make in
            make.top.equalTo(book_titleLabel.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
        }
        
        contentTextView = UITextView()
        contentTextView.backgroundColor = kViewF6Color
        contentTextView.textColor = kColor333333
        contentTextView.layer.cornerRadius = 5
        contentTextView.layer.masksToBounds = true
        contentTextView.delegate = self
//        contentTextView.addTarget(self, action: #selector(changedTextField(_:)), for: .editingChanged)
        bgView.addSubview(contentTextView)
        contentTextView.snp.makeConstraints { make in
            make.top.equalTo(book_authorLabel.snp.bottom).offset(10)
            make.right.equalToSuperview().offset(-40)
            make.left.equalTo(80)
            make.height.equalTo(100)
        }
        
        let contentLabel = UILabel()
        contentLabel.text = "阅读誓言"
        contentLabel.font = UIFont.boldSystemFont(ofSize: 12)
        contentLabel.textColor = kColor666666
        bgView.addSubview(contentLabel)
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(contentTextView).offset(10)
        }
        
        let addButton = UIButton()
        addButton.setTitle("想要读这本书", for: .normal)
        addButton.setTitleColor(.white, for: .normal)
        addButton.backgroundColor = kThemeColor
        addButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        addButton.layer.cornerRadius = 25
        addButton.layer.masksToBounds = true
        addButton.addTarget(self, action: #selector(confirmBtnClick), for: .touchUpInside)
        bgView.addSubview(addButton)
        addButton.snp.makeConstraints { make in
            make.left.equalTo(50)
            make.height.equalTo(50)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-20-view.safeAreaInsets.bottom)
        }
    }
    
    //bgView以外区域
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        if let touch = touches.first {
            let location = touch.location(in: view)
            let convertPoint = view.convert(location, to: bgView)
            if !bgView.bounds.contains(convertPoint) {
                dismissBtnClick()
            }
        }
    }
}

extension HomeEditViewController {
    
    private func setupData(){
        if let imageStr = model?.imageStr {
            if let typeStr = model?.imageType {
                if Int(typeStr) == 1 {
                    if let savedImage = String.convertStrToImage(imageStr) {
                        imageView.image = savedImage
                    }
                } else {
                    imageView.image = UIImage(named: imageStr)
                }
            }
        }
        book_titleLabel.text = model!.book_title ?? ""
        book_authorLabel.text = model!.book_author ?? ""
        
    }
    
    func addButton() {
        
    }
    
    func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
        textView.endEditing(true)
        return true
    }

    func textViewDidChange(_ textView: UITextView) {
        self.book_shiyan = (textView.text ?? "") as NSString
    }
    
    @objc private func dismissBtnClick() {
        
        dismiss(animated: true) {
            
        }
    }
    @objc private func confirmBtnClick() {
        view.endEditing(true)
        if self.book_shiyan.length == 0 {
            let toastText = "请留下阅读\(model?.book_title ?? "")前的足迹吧"
            UIWindow().currentWindow()?.makeToast(toastText, duration: 1.0, position: CSToastPositionCenter)
            return
        }
        
        model?.book_shiyan = String(book_shiyan)
        model?.addTime = Date().timeStamp
        model?.status = "1"
        LocalData().saveAddBookInfoModel(infoModel: model!)
        
        dismissBtnClick()
        UIWindow().currentWindow()?.makeToast("已加入阅读清单！", duration: 2.0, position: CSToastPositionCenter)
    }
}
